package system;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/loadQuestionInf")
public class loadQuestionInf extends HttpServlet {
	private static final long serialVersionUID = 1L;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	int type=Integer.parseInt(req.getParameter("type"));
    	
    	String questionId=req.getParameter("questionId");
    	sqlOperation sql = new sqlOperation();	
    	
    	String result="";
    	String[] inf=null;
    	
    	if(type==1) {
    		inf=sql.getQueryLine("select * from single_selection where question_id='"+questionId+"'",9);
    		result = "{\"topic\":\""+inf[1]+"\",\"A\":\""+inf[2]+"\",\"B\":\""+inf[3]+"\",\"C\":\""+inf[4]+"\",\"D\":\""+inf[5]+"\",\"answer\":\""+inf[6]+"\",\"subject\":\""+inf[7]+"\",\"knowledgePoint\":\""+inf[8]+"\"}";
    	}else if(type==2) {
    		inf=sql.getQueryLine("select * from multiple_selection where question_id='"+questionId+"'",12);
    		result = "{\"topic\":\""+inf[1]+"\",\"A\":\""+inf[2]+"\",\"B\":\""+inf[3]+"\",\"C\":\""+inf[4]+"\",\"D\":\""+inf[5]+"\",\"answer_A\":\""+inf[6]+"\",\"answer_B\":\""+inf[7]+"\",\"answer_C\":\""+inf[8]+"\",\"answer_D\":\""+inf[9]+"\",\"subject\":\""+inf[10]+"\",\"knowledgePoint\":\""+inf[11]+"\"}";
    	}else if(type==3) {
    		inf=sql.getQueryLine("select * from subjective_topic where question_id='"+questionId+"'",9);
    		result = "{\"topic\":\""+inf[1]+"\",\"answer\":\""+inf[2]+"\",\"subject\":\""+inf[3]+"\",\"knowledgePoint\":\""+inf[4]+"\"}";
    	}
    	
    	resp.setContentType("application/json;charset=UTF-8");
		PrintWriter out = resp.getWriter();		
		out.write(result);
    	
    }

}
